package flaud

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/flaud"
	flaudReq "github.com/flipped-aurora/gin-vue-admin/server/model/flaud/request"
)

type CoConspiratorService struct{}

// CreateCoConspirator 创建嫌疑人管理记录
// Author [yourname](https://github.com/yourname)
func (CCService *CoConspiratorService) CreateCoConspirator(ctx context.Context, CC *flaud.CoConspirator) (err error) {
	err = global.GVA_DB.Create(CC).Error
	return err
}

// DeleteCoConspirator 删除嫌疑人管理记录
// Author [yourname](https://github.com/yourname)
func (CCService *CoConspiratorService) DeleteCoConspirator(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&flaud.CoConspirator{}, "id = ?", ID).Error
	return err
}

// DeleteCoConspiratorByIds 批量删除嫌疑人管理记录
// Author [yourname](https://github.com/yourname)
func (CCService *CoConspiratorService) DeleteCoConspiratorByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]flaud.CoConspirator{}, "id in ?", IDs).Error
	return err
}

// UpdateCoConspirator 更新嫌疑人管理记录
// Author [yourname](https://github.com/yourname)
func (CCService *CoConspiratorService) UpdateCoConspirator(ctx context.Context, CC flaud.CoConspirator) (err error) {
	err = global.GVA_DB.Model(&flaud.CoConspirator{}).Where("id = ?", CC.ID).Updates(&CC).Error
	return err
}

// GetCoConspirator 根据ID获取嫌疑人管理记录
// Author [yourname](https://github.com/yourname)
func (CCService *CoConspiratorService) GetCoConspirator(ctx context.Context, ID string) (CC flaud.CoConspirator, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&CC).Error
	return
}

// GetCoConspiratorInfoList 分页获取嫌疑人管理记录
// Author [yourname](https://github.com/yourname)
func (CCService *CoConspiratorService) GetCoConspiratorInfoList(ctx context.Context, info flaudReq.CoConspiratorSearch) (list []flaud.CoConspirator, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&flaud.CoConspirator{})
	var CCs []flaud.CoConspirator
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	var OrderStr string
	orderMap := make(map[string]bool)
	orderMap["name"] = true
	if orderMap[info.Sort] {
		OrderStr = info.Sort
		if info.Order == "descending" {
			OrderStr = OrderStr + " desc"
		}
		db = db.Order(OrderStr)
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&CCs).Error
	return CCs, total, err
}
func (CCService *CoConspiratorService) GetCoConspiratorPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
